'use strict';
/**
 * Created by FreezeSoul on 2015/11/3.
 */
define([
        'angular',
        'app/layout/data-proxy-factory',
        'app/layout/layout-manager',
        'app/modules/page/define',
        'app/modules/page/services/page',
        'app/modules/index/services/index',
        'app/modules/page/controllers/page-layout-free-container'
    ],
    function (angular, ProxyFactory, LayoutManager) {
        angular.module('ad.page')
            .directive('initLayoutPlugin', ["$state", "$uibModal", "adIndexEditService", "adPageEditService", function ($state, $uibModal, adIndexEditService, adPageEditService) {
                return {
                    scope: {
                        pageid: '='
                    },
                    link: function ($scope, $elm, $attrs, $ctrl) {
                        $scope.$watch('pageid', function (newValue, oldValue) {
                            if (newValue) {
                                $scope.layoutContainerEdit = function (option) {
                                    var modalInstance = $uibModal.open({
                                        animation: true,
                                        templateUrl: 'app/modules/page/views/page-layout-free-container.tpl.html',
                                        controller: 'adPageLayoutFreeContainerController',
                                        size: 'lg',
                                        resolve: {
                                            item: function () {
                                                return option;
                                            }
                                        }
                                    });
                                    return modalInstance.result;
                                };

                                $scope.$state = $state;
                                var proxy = new ProxyFactory(adIndexEditService.loadDataSet).create();
                                var layout = new LayoutManager($elm[0], $scope, proxy, adIndexEditService.loadChart, adPageEditService.loadPage);
                                layout.init(newValue, [], true);
                                $scope.$parent.layoutPlugin = layout;
                                $scope.$parent.layoutPlugin.__isEditMode = true;

                                $scope.$parent.bindLayoutEvent();
                            }
                        }, true);

                        var resize = function () {
                            if ($scope.$parent.layoutPlugin) {
                                $scope.$parent.layoutPlugin.resize();
                            }
                        };

                        $('.pages-edit-container,#content').bind('transitionend oTransitionEnd webkitTransitionEnd', resize);

                        $scope.$on('$destroy', function () {
                            $('.pages-edit-container,#content').unbind('transitionend oTransitionEnd webkitTransitionEnd', resize);
                            if ($scope.$parent.layoutPlugin) {
                                $scope.$parent.layoutPlugin.dispose();
                            }
                        });

                        $scope.$on('ad.appWeb.layout.pageLoaded', function (event, arg) {
                            $scope.$parent.Page.page_id = arg.page_id;
                            $scope.$parent.Page.page_name = arg.page_name;
                            $scope.$parent.Page.page_home = arg.page_home;
                            $scope.$parent.Page.page_data = arg.page_data;
                            $scope.$parent.Page.page_folder_id = arg.page_folder_id;
                        });
                    }
                };
            }
            ]);
    });